// DialogWarnings.cpp : implementation file
//

#include "stdafx.h"
#include "isismarc.h"
#include "DialogWarnings.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogWarnings dialog


CDialogWarnings::CDialogWarnings(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogWarnings::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogWarnings)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


CDialogWarnings::~CDialogWarnings()
{
	if (m_pImageList!=NULL)
		delete m_pImageList;
}



void CDialogWarnings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogWarnings)
		// NOTE: the ClassWizard will add DDX and DDV calls here
		DDX_Control(pDX, IDC_LISTAWARNINGS, m_ListaWarnings);
		DDX_Control(pDX, IDOK, m_Ok);
		
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogWarnings, CDialog)
	//{{AFX_MSG_MAP(CDialogWarnings)
	ON_WM_CANCELMODE()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogWarnings message handlers

BOOL CDialogWarnings::OnInitDialog() 
{
	CDialog::OnInitDialog();

	
	// TODO: Add extra initialization here


	SetIcon (AfxGetApp()->LoadIcon(IDR_MAINFRAME),false);
	

	m_pImageList = new CImageList();
	ASSERT(m_pImageList != NULL);    // serious allocation failure checking
	m_pImageList->Create(16, 16, ILC_MASK | ILC_COLOR32,   1, 1);
	m_pImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONCRITICAL));
	m_pImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONWARNING));
	m_pImageList->Add(AfxGetApp()->LoadIcon(IDI_ICON3));


	m_ListaWarnings.SetImageList(m_pImageList, LVSIL_SMALL);
	m_ListaWarnings.SetExtendedStyle ( LVS_EX_FULLROWSELECT );

	
	m_ListaWarnings.InsertColumn(0,"Type", LVCFMT_CENTER,50);
	m_ListaWarnings.InsertColumn(1,"Message", LVCFMT_LEFT,400); 

	CString mimfn;
	mimfn.Format("%d",this->registroselected);

	SetWindowText(this->cmu.databasename+" - Quality Report of MFN:"+mimfn);

	this->RefreshListWarnings();

	

	//m_Ok.EnableWindow(false);

	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogWarnings::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}


void CDialogWarnings::RefreshListWarnings()
{
	CString texto=this->cmu.ServerObtainWarnings(this->registroselected);
	this->processText(texto);

}


void CDialogWarnings::processText(CString mitexto)
{

	mitexto.Replace("<quality>\r\n","");
	mitexto.Replace("</quality>","");


	int poslinea = mitexto.Find("\r\n");
	int i=1;

	while (poslinea>0)
	{
		CString linea = mitexto.Left(poslinea);
		this->processLinea(linea,i++);

		mitexto = mitexto.Mid(poslinea+2);
		poslinea = mitexto.Find("\r\n");
	}

    //m_ListaWarnings.SetFocus();

}


void CDialogWarnings::processLinea(CString linea, int i)
{
	if (linea.Find("CRITICAL")>=0)
	{
		linea.Replace("<CRITICAL>","");
		linea.Replace("</CRITICAL>","");
		int n_Elemento = this->m_ListaWarnings.InsertItem(i,"",0);
		m_ListaWarnings.SetItemText(n_Elemento,1,linea);

	}
	else if (linea.Find("WARNING")>=0)
	{
		
		linea.Replace("<WARNING>","");
		linea.Replace("</WARNING>","");
		int n_Elemento = this->m_ListaWarnings.InsertItem(i,"",1);
		m_ListaWarnings.SetItemText(n_Elemento,1,linea);
		//int n_Elemento = m_ListaWarnings.InsertItem(i,linea);

	}
	else
	{
		
		linea.Replace("<ABCD>","");
		linea.Replace("</ABCD>","");
		int n_Elemento = this->m_ListaWarnings.InsertItem(i,"",2);
		m_ListaWarnings.SetItemText(n_Elemento,1,linea);
		//int n_Elemento = m_ListaWarnings.InsertItem(i,linea);

	}

}

void CDialogWarnings::AssignData(CMyIsisExternal cmi, int reg)
{
	this->cmu=cmi;
	this->registroselected = reg;

}

void CDialogWarnings::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnClose();
}
